﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Sunboxsoft.ASMS.Integration
{
    public static class MobileRechargeServiceProxy
    {
        public static User ValidateCustomer(string telephone)
        {
            MobileRechargeServiceRef.MobileRechargeServiceClient client = new MobileRechargeServiceRef.MobileRechargeServiceClient();

            /*
            Logger.Write(this.GetType(), EnumLogLevel.Info, "请求参数：" + requestXML);
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(requestXML);
            string FlowNo = ParseXML(xml, "FlowNo");
            string UserType = ParseXML(xml, "UserType");
            string UserID = ParseXML(xml, "UserID");
            string str = "FlowNo=" + FlowNo + "&UserType=" + UserType + "&UserID=" + UserID;
            Logger.Write(this.GetType(), EnumLogLevel.Info, "准备加密：" + str);
            AddMac(xml, str);
            Logger.Write(this.GetType(), EnumLogLevel.Info, "处理后参数：" + xml.OuterXml);
            */
            /*<?xml version="1.0" encoding="utf-8" ?>
<req>
<FlowNo>流水号</FlowNo>
<UserType>用户类型（01会员号；02手机号码；03微信ID；04电商账号 ）</UserType>
<UserID>用户ID</UserID>
<Mac>报文校验位</Mac>
<req>*/
            XDocument xDoc = new XDocument();
            xDoc.Declaration = new XDeclaration("1.0", "utf-8", "yes");
            XElement xReq = new XElement("req");
            XElement xFlowNo = new XElement("FlowNo");
            xFlowNo.Value = "checkvipcustomer";
            XElement xUserType = new XElement("UserType");
            xUserType.Value = "02";
            XElement xUserID = new XElement("UserID");
            xUserID.Value = telephone;
            XElement xMac = new XElement("Mac");
            string str = "FlowNo=" + xFlowNo.Value + "&UserType=" + xUserType.Value + "&UserID=" + xUserID.Value;
            xMac.Value = GetMD5(str); ;

            xReq.Add(xReq);
            xReq.Add(xFlowNo);
            xReq.Add(xUserType);
            xReq.Add(xUserID);
            xReq.Add(xMac);
            xDoc.Add(xReq);
            string requestXml = xDoc.ToString();
            string responseXml = client.GetUserInfoByUId(requestXml);

            User user = null;
            using (StringReader reader = new StringReader(responseXml))
            {
                try
                {
                    XDocument xResult = XDocument.Load(reader);
                    XElement xUser = xResult.Element("res").Element("Users");
                    user = Newtonsoft.Json.JsonConvert.DeserializeObject<User>(xUser.Value);
                    user.UserName = System.Web.HttpUtility.UrlDecode(user.UserName);
                }
                catch (Exception ex)
                {
                    user = null;
                }
            }

            return user;
        }

        private static string GetMD5(string source)
        {
            byte[] result = Encoding.Default.GetBytes(source);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            StringBuilder byte2String = new StringBuilder();

            for (int i = 0; i < output.Length; i++)
            {
                byte2String.Append(output[i].ToString("X2"));
            }
            return byte2String.ToString();
        }
    }
}
